// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import yyComponent from "../Common/yyComponent";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MajiangAnimation extends yyComponent {

    @property(cc.Node)
    majiangArr:Array <cc.Node> = []

    @property(cc.Node)
    renwu:cc.Node = null

    @property(cc.Node)
    zoumajiangArr:Array <cc.Node> = []

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    skinName = ['中黄东', '天蓝冬', '橙秋', '粉南', '紫春', '红中', '绿夏', '绿灰发', '蓝西', '黄北'];
    nowSkin = ''
    nowNumber = 0
    start () {
        
        this.rSkin()
        this.addmj()
        this.schedule(this.addmj,4.5)
        
    }

    addmj(){
        this.renwu.active = true
        this.renwu.x = -400
        this.scheduleOnce(()=>{
            this.renwu.getComponent(sp.Skeleton).setSkin(this.nowSkin)
        })
        this.renwu.getComponent(sp.Skeleton).setAnimation(0,'zoulu',true)
        cc.tween(this.renwu).to(1.5,{x:0}).call(()=>{
            this.renwu.getComponent(sp.Skeleton).setAnimation(0,'daiji',true)
            
        }).delay(0.3).to(0.2,{opacity:0}).call(()=>{
            this.renwu.active = false
            this.renwu.opacity = 255
            this.shangzhuo()
           
        }).start()
    }

    rSkin(){
        let nowSkin = this.skinName[Math.floor(Math.random()*this.skinName.length)] 
        this.nowSkin = nowSkin
      
    }

    shangzhuo(){
        this.nowNumber++;

        for (let i = 0; i < this.nowNumber; i++) {
            const element = this.majiangArr[i];
            if(element){
                
                if(!element.active){
                    element.opacity = 0                    
                }
                element.active = true
                cc.tween(element).to(.2,{opacity:255}).start()
                this.scheduleOnce(()=>{
                    element.getComponent(sp.Skeleton).setSkin(this.nowSkin)
                })
            }
        }

        if(this.nowNumber>=4){
            this.scheduleOnce(()=>{
                this.node.getChildByName('hule').active = true
                this.node.getChildByName('hule').getComponent(sp.Skeleton).setAnimation(0,'animation',false)
                this.scheduleOnce(()=>{
                    this.node.getChildByName('hule').active = false
                },1)
    
                for (const iterator of this.majiangArr) {
                    cc.tween(iterator).to(.2,{opacity:0}).call(()=>{
                        iterator.active = false
                        iterator.opacity = 255
                    }).start()
                }
    
                for (let i = 0; i < this.zoumajiangArr.length; i++) {
                    const element = this.zoumajiangArr[i];
                    if(element){
                        element.opacity = 0
                        element.active = true
                        cc.tween(element).to(.2,{opacity:255}).start()

                        element.x = -200 + i*100
                        element.y = -170
                        element.getComponent(sp.Skeleton).setAnimation(0,'zoulu',true)
                        this.scheduleOnce(()=>{
                            element.getComponent(sp.Skeleton).setSkin(this.nowSkin)
                        })
                        cc.tween(element).by(1.5,{x:1000}).call(()=>{
                            element.active = false
                        }).start()
                    }
                }
                this.scheduleOnce(()=>{
                    this.rSkin()
                },1)
            },1)
          
            this.nowNumber = 0
        }
    }

    // update (dt) {}
}
